package com.eascs.web.o2o.scheduler.service;

import java.util.List;

import javax.inject.Inject;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.web.o2o.dc.dao.ServiceDao;
import com.eascs.web.o2o.scheduler.dao.CrmDao;
import com.eascs.web.o2o.scheduler.entity.CustomerAssetInfo;
import com.eascs.web.o2o.scheduler.entity.CustomerAssetInfoCrm;
import com.eascs.web.o2o.scheduler.entity.CustomerShopInfo;
import com.eascs.web.o2o.scheduler.entity.CustomerShopInfoCrm;
import com.eascs.web.o2o.scheduler.entity.O2oLoanUser;
import com.eascs.web.o2o.scheduler.filter.AssetTrans;

/**
* @author bao.pan 2017年7月11日 下午3:57:58
*/
@Service
public class CrmByServiceService {
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	@Inject
	ServiceDao serviceDao;
	@Inject
	CrmDao crmDao;
	/*
	 * customer_asset_info同步
	 */
	public void CrmByService(){
		String deleteSql = "DELETE FROM customer_asset_info";
		logger.info("删除业务表customer_asset_info数据开始=============");
		int num=serviceDao.execute(deleteSql);
		logger.info("删除业务表customer_asset_info数据完成=============count={}",num);
		String querySql = " select * from customer_asset_info group by customer_id  ";
		int pageIndex = 0;
		int pageSize = 100;
		while (pageIndex >= 0) {
			logger.info("业务表customer_asset_info表数据入库任务运行中，正在查询第[{}]页数据", pageIndex);
			SqlPageQuery<CustomerAssetInfoCrm> sqlPageQuery = new SqlPageQuery<>(CustomerAssetInfoCrm.class, pageSize,
					pageIndex, querySql);
			PageResult<CustomerAssetInfoCrm> results = crmDao.findDataWithPageForMysql(sqlPageQuery);

			List<CustomerAssetInfoCrm> list = results.getData();
			for (CustomerAssetInfoCrm crm : list) {
				CustomerAssetInfo cus=AssetTrans.customerAssetInfoTrans(crm);
				List<O2oLoanUser> o2olist=o2olistbyloanid(cus.getCustomerId());
				for (O2oLoanUser o2oLoanUser : o2olist) {
					cus.setLoanapplyNo(o2oLoanUser.getLoanapplyNo());
					serviceDao.insert(cus);
				}
			}
			pageIndex++;
			if (results.getPageTotal() <= pageIndex) {
				pageIndex = -1;
			}
		}
		logger.info("业务表customer_asset_info表数据入库任务结束=============");
	}
	
	private List<O2oLoanUser> o2olistbyloanid(String loanid){ 
		String sql="SELECT * from o2o_loan_user where spouse_id is null and sponsor_type is NULL and  loan_id=? ";
		return serviceDao.findEntitys(sql, O2oLoanUser.class, loanid);
	}
	
	
	/**
	 * @Title: sych_customer_shop_info
	 * @Description: 同步门店信息
	 * @author 张庆奇
	 * @return void
	 * @throws
	 * @date 2017年7月14日 下午1:24:04
	 */
	public void sych_customer_shop_info(){
		String deleteSql = "DELETE FROM customer_shop_info";
		logger.info("删除业务表customer_shop_info数据开始=============");
		//int num=serviceDao.execute(deleteSql);
		//logger.info("删除业务表customer_shop_info数据完成=============count={}",num);
		String querySql = " select * from customer_shop_info group by customer_shop_id  ";
		int pageIndex = 0;
		int pageSize = 100;
		while (pageIndex >= 0) {
			logger.info("业务表customer_shop_info表数据入库任务运行中，正在查询第[{}]页数据", pageIndex);
			SqlPageQuery<CustomerShopInfoCrm> sqlPageQuery = new SqlPageQuery<>(CustomerShopInfoCrm.class, pageSize,
					pageIndex, querySql);
			PageResult<CustomerShopInfoCrm> results = crmDao.findDataWithPageForMysql(sqlPageQuery);

			List<CustomerShopInfoCrm> list = results.getData();
			for (CustomerShopInfoCrm crm : list) {
				CustomerShopInfo cus=new CustomerShopInfo();
				BeanUtils.copyProperties(crm, cus);
				List<O2oLoanUser> o2olist=o2olistbyloanid(crm.getCustomerId());
				for (O2oLoanUser o2oLoanUser : o2olist) {
					cus.setCustomerShopIdCrm(cus.getCustomerShopId());
					cus.setLoanapplyNo(o2oLoanUser.getLoanapplyNo());
					cus.setDataSource("2");
					cus.setCustomerShopId(null);
					serviceDao.insert(cus);
				}
			}
			pageIndex++;
			if (results.getPageTotal() <= pageIndex) {
				pageIndex = -1;
			}
		}
		logger.info("业务表customer_shop_info表数据入库任务结束=============");
	}
	
	
	
	
}
